package com.yuerlive.rainbow.module.login.presenter;

import com.yuerlive.rainbow.API;
import com.yuerlive.rainbow.api.LoginApi;
import com.yuerlive.rainbow.api.LoginService;
import com.yuerlive.rainbow.api.UserApi;
import com.yuerlive.rainbow.api.UserService;
import com.yuerlive.rainbow.base.BasePresenter;
import com.yuerlive.rainbow.base.BasePresenterImpl;
import com.yuerlive.rainbow.entity.BaseData;
import com.yuerlive.rainbow.entity.EditedInfo;
import com.yuerlive.rainbow.entity.User;
import com.yuerlive.rainbow.entity.UserInfo;
import com.yuerlive.rainbow.rx.RxHelper;
import com.yuerlive.rainbow.rx.RxObserver;
import com.yuerlive.rainbow.utils.L;
import com.yuerlive.rainbow.utils.UserProxy;
import com.yuerlive.rainbow.widget.timeselector.TextUtil;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by xiaoyi on 2017/5/23.
 */

public class LoginPresenter extends BasePresenterImpl implements LoginContract.Presenter {
    LoginContract.View view;
    LoginService service;
    UserService userService;

    public LoginPresenter(LoginContract.View view) {
        this.view = view;
        service = LoginApi.getInstance().loginService;
        this.view.setPresenter(this);
        userService = UserApi.getInstance().service;
    }

    @Override
    public void getSmsCode(String phone, String code,int type) {
        Map<String, String> params = new HashMap<>();
        params.put("mobile", phone);
        params.put("picCode", code);
        params.put("type", String.valueOf(type));
        view.showLoading();
        service.sendSmsCode(params).compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData>(this) {
                    @Override
                    protected void onSuccess(BaseData baseData) {
                        L.e("LoginPresenter", " onSuccess   baseData " + baseData.getCode() + "::" + baseData.getResult());
                        view.sendSuccess(baseData);
                    }

                    @Override
                    protected void onFail(String message) {
                        L.e("LoginPresenter", " onFail   message " + message);
                        view.showFail(message, API.SEND_SMS_CODE);
                    }
                });
    }

    @Override
    public void getImgCode() {

    }

    @Override
    public void login(String phone, String code) {
        Map<String, String> params = new HashMap<>();
        params.put("mobile", phone);
        params.put("smsCode", code);
        view.showLoading();
        service.login(params).subscribeOn(Schedulers.io()).flatMap(new Function<BaseData<User>, ObservableSource<BaseData<UserInfo>>>() {
            @Override
            public ObservableSource<BaseData<UserInfo>> apply(@NonNull BaseData<User> userBaseData) throws Exception {
                if (userBaseData.getCode() == 0) {
                    params.clear();
                    params.put("otherId", userBaseData.getObject().getUserId());
                    UserProxy.setUser(userBaseData.getObject(), false);
                    return userService.getUserInfo(params);
                } else {
                    view.loginFaild(userBaseData.getCode(), userBaseData.getResult());
                    return null;
                }

            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new RxObserver<BaseData<UserInfo>>(this) {
            @Override
            protected void onSuccess(BaseData<UserInfo> data) {
                User user = UserProxy.getUser();
                user.setUserInfo(data.getObject());
                view.showLoading();
                view.loginResult(user);
                UserProxy.setUser(user);
            }

            @Override
            protected void onFail(String message) {

            }
        });

    }

    @Override
    public void completeInfo(String headIcon, String nickname, int sex) {
        User user = UserProxy.getUser();
        Map<String, String> params = new HashMap<>();
        params.put("headIcon", headIcon);
        params.put("nickname", nickname);
        params.put("sex", String.valueOf(sex));
        service.completeInfo(params).compose(RxHelper.toUI()).subscribe(new RxObserver<BaseData>(this) {
            @Override
            protected void onSuccess(BaseData baseData) {
                view.sendSuccess(baseData);
            }

            @Override
            protected void onFail(String message) {

            }
        });

    }

    @Override
    public void getEditRequest(String mobile, String verificationCode) {
        User user = UserProxy.getUser();
        Map<String, String> params = new HashMap<>();
        params.put("mobile", mobile);
        params.put("smsCode",verificationCode);
        service.getEditRequest(params).compose(RxHelper.toUI()).subscribe(new RxObserver<BaseData>(this) {
            @Override
            protected void onSuccess(BaseData baseData) {
                view.bindMobileSuccess(baseData);
            }

            @Override
            protected void onFail(String message) {

            }
        });
    }

    @Override
    public void thirdLogin(String icon, String nickname, String openId, String platform, String sex, String source) {
        Map<String, String> params = new HashMap<>();
        params.put("icon", icon);
        params.put("nickname", nickname);
        params.put("openId", openId);
        params.put("platform", platform);
        params.put("sex", sex);
        params.put("source", source);
        L.e("LoginPresenter", "thirdLogin");
        service.thirdLogin(params).subscribeOn(Schedulers.io()).flatMap(new Function<BaseData<User>, ObservableSource<BaseData<UserInfo>>>() {
            @Override
            public ObservableSource<BaseData<UserInfo>> apply(@NonNull BaseData<User> userBaseData) throws Exception {
                params.clear();
                params.put("otherId", userBaseData.getObject().getUserId());
                UserProxy.setUser(userBaseData.getObject(), false);
                return userService.getUserInfo(params);
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new RxObserver<BaseData<UserInfo>>(this) {
            @Override
            protected void onSuccess(BaseData<UserInfo> data) {
                User user = UserProxy.getUser();
                user.setUserInfo(data.getObject());
                view.showLoading();
                view.loginResult(user);
                UserProxy.setUser(user);
            }

            @Override
            protected void onFail(String message) {

            }
        });
    }
}
